<template>
  <div class="app-container">
    <div class="title">讲师列表</div>
    <el-form :inline="true" :model="teacherQuery" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="teacherQuery.name" placeholder="讲师名"> </el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="teacherQuery.level" placeholder="讲师头衔">
          <el-option label="高级讲师" :value="1"></el-option>
          <el-option label="首席讲师" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker
          v-model="teacherQuery.begin"
          type="date"
          placeholder="选择开始时间"
        ></el-date-picker>
        <el-date-picker
          v-model="teacherQuery.end"
          type="date"
          placeholder="选择截止时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getList()"
          >查询</el-button
        >
        <el-button @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="70">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="名称" width="70">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="头衔" width="80" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.level === 1 ? "高级讲师" : "首席讲师" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资历" prop="intro" align="center">
      </el-table-column>
      <el-table-column
        class-name="status-col"
        label="添加时间"
        width="152"
        align="center"
        prop="gmtCreate"
      >
      </el-table-column>
      <el-table-column align="center" prop="sort" label="排序" width="60">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <router-link :to="`/teacher/edit/${scope.row.id}`">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="deleteTeacher(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      style="text-align: center; margin-top: 20px"
      @current-change="getList"
      :page-size="limit"
      layout="total, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script>
import teacherApi from "@/api/teacher";
export default {
  data() {
    return {
      list: null, //当前列表所需要的数据
      listLoading: true, //转‘菊花’的效果
      page: 1, //当前页
      limit: 10, //每页记录数
      teacherQuery: {}, //条件查询对象
      total: 0, //总的记录数
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList(page = 1) {
      //获取当前的页码
      this.page = page;
      //调用获取讲师列表的APi
      this.listLoading = true;
      teacherApi
        .getList(this.page, this.limit, this.teacherQuery)
        .then((res) => {
          console.log(res);
          //获取api中的数据
          this.list = res.data.items;

          //获取总记录数
          this.total = res.data.totalCount;
          //让‘菊花’停止
          this.listLoading = false;
        });
    },
    resetData() {
      //将查询表单的对象属性清空
      this.teacherQuery = {};
      //调用获取讲师列表的api
      this.getList();
    },
    //删除讲师
    deleteTeacher(id) {
      //根据id删除讲师
      //先确定是否要删除
      this.$confirm("您确定要删除该讲师吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //调用讲师的api
          teacherApi.deleteById(id).then((res) => {
            //删除成功,提示信息
            this.$message({
              type: "success",
              message: "删除成功",
            });
            //重新加载数据
            this.getList();
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
.title {
  font-weight: 700;
  font-size: 22px;
  color: #9a5f04;
  line-height: 40px;
}
</style>